﻿@{
    ViewBag.Title = "";
    var id = Model != null ? Model.ID : 0;
}
@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.WebProvider
@model ZBJF.Warranty.Model.NoFKDbEntity.InsuranceProductPackageNOFK
@section Header{
    @Styles.Render("~/Content/chosen")
}
<ul class="nav nav-tabs">
    <li class="Add-R"><a href="/SellerInsuranceProducts/">保险卡产品列表</a></li>
    <li class="active"><a>@Html.Label(id == 0 ? "新建保险卡产品" : "编辑保险卡产品")</a></li>
</ul>
<h4>保险卡编辑</h4>
<hr/>
@using (Html.BeginForm("SaveInsuranceProductPackage", "SellerInsuranceProducts", FormMethod.Post, new {id = "mvcForm", name = "mvcForm", @class = "form-horizontal", enctype = "multipart/form-data"}))
{
      @Html.HiddenFor(m=>m.ID)
    <table class="Main_tab">
        <tr>
            <th>保险卡名称：</th>
            <td style="width: 250px;">
                @Html.TextBoxFor(m => m.ProductPackageName, new {maxlength = 20,  validate = "{required:true,messages:{required:'请输入保险卡名称！'}}"})
            </td>
            <th>激活期限</th>
            <td>
                @Html.TextBoxFor(m => m.ActiveCardInLimitDays, new { maxlength = 20, validate = "{required:true,messages:{required:'请输入激活期限！'}}" }) 天
            </td>
        </tr>
    </table>
    @Html.Hidden("insuranceProductsIdJson")
    <br/>
    <div class="table-responsive dataTables_wrapper">
        <table id="sellersList" class="table table-striped table-bordered table-hover">
            <thead>
            <tr>
                <th width="20">序号</th>
                <th width="80">保险产品名称</th>
                <th width="100">操作</th>
            </tr>
            </thead>
            <tbody id="sellersTr">
            @for (int j = 0; j < Model.InsuranceProductPackageMappings.Count; j++)
{
    <tr class="text-center" id="trInsuranceProductPackageMappings_@(j)">
        <td>
            @(j + 1)
        </td> 
        <td>
            @Html.HiddenFor(m => Model.InsuranceProductPackageMappings[j].InsuranceProductId)
            @Html.TextBoxFor(m => Model.InsuranceProductPackageMappings[j].InsuranceProductName, new { validate = "{required:true,messages:{required:'请选择保险产品'}}", @readonly = "readonly", @class = "select-icon w-p39", onclick = "showInsuranceProductLayer(this)" })
        </td>
        <td>
            <a class='Edit-R ' href="javascript:void(0)" title='delete' onclick="removeInsuranceProductPackageMappings(trInsuranceProductPackageMappings_@(j))"><i class='icon-remove'></i></a>
        </td>
    </tr>
}
            </tbody>
            <tfoot>
            <tr>
                <td colspan="4">
                    <input id="btnAddNewRow" class="btn btn-primary Edit-R" type="button" value="添加保险基础产品">
                </td>
            </tr>
            </tfoot>
        </table>
        <div style="text-align: center; margin-top: 100px;">
            <input id="btnAdd" class="btn btn-primary Edit-R" type="submit" value="保存" onclick="Save()">
            <input id="btnClose" class="btn btn-primary Edit-R" type="reset" value="关闭" onclick="javascript: window.close(); window.location.href = '/SellerInsuranceProducts/Index';">
        </div>
    </div>
}@section scripts{
     <script>
         var index, i = 0;
         //增加行
         $("#btnAddNewRow").click(function() {
             var html = $(document.getElementById("sellersList").rows[1]).html();
             var lastTrId = $("#sellersList tbody tr:last-child").attr("id");
             var newId = parseInt(lastTrId.substring(lastTrId.indexOf('_') + 1));
             var regExpOrderItem = new RegExp("0", "g"); //第一行序号都是0
             html = html.replace(regExpOrderItem, newId + 1);

             html = "<tr id='trInsuranceProductPackageMappings_" + (newId + 1) + "'>" + html + "</tr>";
             $("#sellersList tbody").append(html);
             $("#sellersList tbody tr:last-child input").val("");
             $("#sellersList tbody tr:last-child td:first").html(newId + 2);
         });
         //删除行
         function removeInsuranceProductPackageMappings(trInsuranceProductPackageMappingsId) {            
             if (($("#sellersList tbody tr").length == 1)) {
                 layer.alert("最后一行不能删除，至少保留一行！");
                 return;
             }
             $(trInsuranceProductPackageMappingsId).remove();
             var i = 0;
             $("#sellersList tbody tr").each(function () {
                 $(this).find("td:first").html(i + 1);
                 $(this).find("td:last").html("<a class='Edit-R ' href=\"javascript:void(0)\" title='delete' onclick=\"removeInsuranceProductPackageMappings(trInsuranceProductPackageMappings_" + i + ")\"><i class='icon-remove'></i></a>");
                 $(this).attr("id", "trInsuranceProductPackageMappings_" + i);
                 $(this).find("input").each(function () {
                     var regExpID = new RegExp("_\\d\+__", "g"); //替换ID
                     var regExpName = new RegExp("\\[\\d\+\\]", "g"); //替换Name

                     var id = $(this).attr("id");
                     var name = $(this).attr("name");

                     $(this).attr("id", id.replace(regExpID, "_" + i + "__"));
                     $(this).attr("name", name.replace(regExpName, "[" + i + "]"));

                     var clickEvent = $(this).attr("onclick");
                     if (clickEvent && clickEvent != "")
                         $(this).attr("onclick", clickEvent.replace(regExpID, "_" + i + "__"));

                     var onkeyupEvent = $(this).attr("onkeyup");
                     if (onkeyupEvent && onkeyupEvent != "")
                         $(this).attr("onkeyup", onkeyupEvent.replace(regExpID, "_" + i + "__"));
                 });
                 i++;
             });
         }

         var objid;

         function showInsuranceProductLayer(obj) {
             objid = obj;
             layer.open({
                 type: 2,
                 title: '请选择保险产品',
                 shadeClose: true,
                 shade: 0.8,
                 area: ['820px', '75%'],
                 content: '/SellerInsuranceProducts/AddInsuranceProducts?Type=1' //iframe的url
             });
         }

         //选中客户后设置值,清空选中的配件
         function setSelectInsuranceProduct(item) {
             var i = 0;
             var status = false;
             $("#sellersTr").children().siblings().each(function () {
                 if ($("#InsuranceProductPackageMappings_" + i + "__InsuranceProductId").val() == item[0].ID) {
                     status = true;
                 }
                 i++;
             });
             if (status) {
                 layer.alert("重复数据");
                 return false;
             }            
             if (!item || item == "") {
                 $(objid).prev().val("");
                 $(objid).val("");
             } else {
                 $(objid).prev().val(item[0].ID);
                 $(objid).val(item[0].ProductName);
             }
         }
     </script>
 }